package com.youxue.service;

import com.youxue.dao.ClassTypeMapper;
import com.youxue.entity.ClassType;
import com.youxue.entity.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 15c on 2018/1/17.
 */
@Service("ClassType")
public class ClassTypeServiceImpl implements ClassTypeService {
    @Autowired
    ClassTypeMapper classTypeMapper;

    @Override
    public List<ClassType> showAllRootClassType() {
        return classTypeMapper.selectAllRootClassType();
    }

    @Override
    public List<ClassType> showAllSecondClassType(Integer pid) {
        return classTypeMapper.selectAllSecondClassType(pid);
    }

    @Override
    public Pager<ClassType> showAllSecondClassTypeByPage(Integer pid, Integer pageOffset) {
        //一页多少条数据
        int pageSize = 6;
        Pager<ClassType> pager = new Pager<ClassType>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = classTypeMapper.selectAllSecondClassTypeCoutByPage(pid);
        pager.setTotalRecord(totalRecord);
        pager.setDatas(classTypeMapper.selectAllSecondClassTypeByPage(pid,pageOffset, pageSize));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }

        return pager;
    }

    @Override
    public int addARootClassType(ClassType classType) {
        return classTypeMapper.insertSelective(classType);
    }

    @Override
    public ClassType showAClassType(Integer typeId) {
        return classTypeMapper.selectByPrimaryKey(typeId);
    }

    @Override
    public int updateAClassType(ClassType classType) {
        return classTypeMapper.updateByPrimaryKeySelective(classType);
    }
}
